DELETE FROM TBL_CARD_DAILY_RAW
  
	  INSERT TBL_CARD_DAILY_RAW
SELECT A.[BUSINESS_DATE]
      ,A.[CIF]
      ,A.[CUS_NAME]
	  ,B.BRANCH_ID
      ,[ACNT_CONTRACT_ID]
      ,[ACNT_CONTRACT_OID]
      ,[PRODUCT_GROUP]
      ,[CONTRACT_NUMBER]
      ,[CONTRACT_DATE_OPEN]
      ,[CONTRACT_TYPE]
      ,[ACCOUNT_LIMIT]
      ,[CONTRACT_STATUS]
      ,[CARD_NUMBER]
      ,[CARD_DATE_OPEN]
      ,[CARD_TYPE]
      ,[CARD_LIMIT]
      ,[CARD_STATUS]
      ,[PRODUCTION_STATUS]
      ,[UNLOCK_DATE]
      ,[ACTUAL_DATE_EXPIRE]
      ,ISNULL([BALANCE],0) AS BALANCE
      ,[SOURCE_ID]
      ,B.DAO
      ,B.DAO_NAME
      ,[TYPE_CARD]
      ,[UNLOCK_CARD]
      ,[SCORED_CARD]
FROM SERVER12.TRANGLT9.[DBO].[CARD_DAILY_SME] A, TBL_CUSTOMER B
WHERE A.CIF = B.CIF

--SELECT * FROM TBL_CARD_DAILY_RAW

--DROP TABLE TBL_CM_CARD

DELETE FROM TBL_CM_CARD_NEW

  INSERT TBL_CM_CARD_NEW
  SELECT A.BUSINESS_DATE,A.CIF, DAO, SUM(SCORED_CARD) AS SCORED_CARD, SUM(ISNULL(BALANCE, 0)) AS BALANCE, COUNT(CARD_NUMBER) AS NO_OF_CARD
  FROM TBL_CARD_DAILY_RAW A
  WHERE [UNLOCK_CARD] <>'NOT_OK'
  GROUP BY A.BUSINESS_DATE,A.CIF, DAO
  ORDER BY NO_OF_CARD

  --SELECT * FROM SERVER12.TRANGLT9.[DBO].[CARD_DAILY_SME] WHERE CIF = '3067972'
  --SELECT * FROM TBL_CM_CARD_NEW

  --SELECT * FROM TBL_CM_CARD_NEW ORDER BY NO_OF_CARD
  --SELECT * FROM TBL_HR A, TBL_BRANCH B WHERE DAO = '13543' AND A.BRANCH_CODE_SME = B.BRANCH_CODE_SME